/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.task;

import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.guide.entity.*;
import com.jeeplus.modules.guide.service.DockingOtherService;
import com.jeeplus.modules.guide.service.MobileLoginService;
import com.jeeplus.modules.guide.service.MobileOtherService;
import com.jeeplus.modules.guide.service.MobileQueueService;
import com.jeeplus.modules.sys.utils.FileKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.List;

/*
第三方对接接口
*/
@Component

public class QueueOther{
	@Autowired
	private MobileOtherService mobileOtherService;
	@Autowired
	private MobileQueueService mobileQueueService;
	@Autowired
	private MobileLoginService mobileLoginService;
	@Autowired
	private DockingOtherService dockingOtherService;
	private static final Logger log = LoggerFactory.getLogger(QueueOther.class);

	/**
	 * 添加排队等待信息
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	public void intoOtherQueue() {
		log.error("添加排队记录");
		List<MobileOtherQueue> list =  mobileQueueService.getMobileOtherQueue();
		if(list.size() >= 1){
			mobileOtherService.deleteQueue();
			mobileOtherService.intoOtherQueue(list);
		}
	}

	/**
	 * 添加排队办理信息
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	public void intoOtherHQueue() {
		mobileQueueService.updateTimeIsNull();
		List<MobileOtherHQueue> list = mobileQueueService.getMobileOtherHQueue();
		for(MobileOtherHQueue mh : list){
			try{
				if(mobileOtherService.intoOtherQueueHistory(mh,DateUtils.getDateTime()) >= 1){
					mobileQueueService.updateOtherHQueue(mh.getId(),DateUtils.getDateTime());
				}
			}catch (Exception e){
				log.error(mh.getId()+"失败："+e.getMessage());
			}

		}
	}



	/**
	 * 添加窗口信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	public void intoCounter(){
		log.error("添加窗口记录");
		List<MobileCounter> mobileCounter = mobileQueueService.getMobileCounter();
		mobileOtherService.deleteCounter();
		mobileOtherService.intoCounter(mobileCounter);

	}
	/**
	 * 添加员工信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	public void intoQueueUser(){
		log.error("添加员工记录");
		List<QueueUser> queueUsers = mobileQueueService.queueUserList();
		mobileOtherService.delQueueUsers();
		mobileOtherService.intoQueueUsers(queueUsers);

	}

	/**
	 * 添加点位信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	public void intoOrgrizeTree(){
		log.error("添加点位记录");
		List<MobileOrgrizeTree> mobileOrgrizeTree = mobileQueueService.getMobileOrgrizeTree();
		mobileOtherService.deleteOrgrizeTree();
		mobileOtherService.intoOrgrizeTree(mobileOrgrizeTree);
	}
	/**
	 * 添加业务信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	public void intoSerial(){
		log.error("添加业务记录");
		List<MobileSerial> mobileSerial = mobileQueueService.getMobileSerial();
		mobileOtherService.deleteSerial();
		mobileOtherService.intoSerial(mobileSerial);
	}

	/**
	 * 添加业务主题信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	public void intoManager(){
		log.error("添加业务主题记录");
		List<MobileManager> mobileManager = mobileQueueService.getMobileManager();
		mobileOtherService.deleteManager();
		mobileOtherService.intoManager(mobileManager);
	}
	/**
	 * 添加业务信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	public void intoPjProject(){
		log.error("添加评价记录");
		List<MobilePjProject> mobilePjProject = mobileQueueService.getMobilePjProject();
		mobileOtherService.deletePjProject();
		mobileOtherService.intoPjProject(mobilePjProject);
	}

	/**
	 * 添加注册群众照片复印件 身份证反面
	 *
	 * @author
	 * @version 2020-04-24
	 */
	@Async()
	public void toFileOcrBack(String OcrBack,String openid){
		BufferedOutputStream bos = null;
		java.io.FileOutputStream fos = null;
		File file = null;
		byte[] bytes = null;
		try {
			String realPath = FileKit.getUserAttachmentDir(openid);
			bytes = Base64.getDecoder().decode(OcrBack);
			file = new File(realPath + "身份证反面.png");
			fos = new java.io.FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
			if(mobileLoginService.updateUserMaterial("/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "身份证反面.png",openid,"身份证反面") <= 0){
				mobileLoginService.intoUserMaterial("身份证反面","/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "身份证反面.png",openid);
			}else{
				mobileLoginService.delUserMaterial(openid);
			}
		} catch (Exception e) {
			log.error(openid+"上传身份证反面失败！reson:"+e.getMessage());
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * 添加注册群众照片复印件 身份证正面
	 *
	 * @author
	 * @version 2020-04-24
	 */
	@Async()
	public void toFileOcrFront(String OcrFront,String openid){
		BufferedOutputStream bos = null;
		java.io.FileOutputStream fos = null;
		File file = null;
		byte[] bytes = null;
		try {
			String realPath = FileKit.getUserAttachmentDir(openid);
			bytes = Base64.getDecoder().decode(OcrFront);
			file = new File(realPath + "身份证正面.png");
			fos = new java.io.FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
			if(mobileLoginService.updateUserMaterial("/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "身份证正面.png",openid,"身份证正面") <= 0){
				mobileLoginService.intoUserMaterial("身份证正面","/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "身份证正面.png",openid);
			}else{
				mobileLoginService.delUserMaterial(openid);
			}
		} catch (Exception e) {
			log.error(openid+"上传身份证正面失败！reson:"+e.getMessage());
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * 添加注册群众照片复印件 身份证反面
	 *
	 * @author
	 * @version 2020-04-24
	 */
	@Async()
	public void toFileOcrBackFR(String OcrBack,String openid){
		BufferedOutputStream bos = null;
		java.io.FileOutputStream fos = null;
		File file = null;
		byte[] bytes = null;
		try {
			String realPath = FileKit.getUserAttachmentDir(openid);
			bytes = Base64.getDecoder().decode(OcrBack);
			file = new File(realPath + "法人身份证反面.png");
			fos = new java.io.FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
			if(mobileLoginService.updateUserMaterial("/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "法人身份证反面.png",openid,"法人身份证反面") <= 0){
				mobileLoginService.intoUserMaterial("法人身份证反面","/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "法人身份证反面.png",openid);
			}else{
				mobileLoginService.delUserMaterial(openid);
			}
		} catch (Exception e) {
			log.error(openid+"上传法人身份证反面失败！reson:"+e.getMessage());
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * 添加注册群众照片复印件 身份证正面
	 *
	 * @author
	 * @version 2020-04-24
	 */
	@Async()
	public void toFileOcrFrontFR(String OcrFront,String openid){
		BufferedOutputStream bos = null;
		java.io.FileOutputStream fos = null;
		File file = null;
		byte[] bytes = null;
		try {
			String realPath = FileKit.getUserAttachmentDir(openid);
			bytes = Base64.getDecoder().decode(OcrFront);
			file = new File(realPath + "法人身份证正面.png");
			fos = new java.io.FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
			if(mobileLoginService.updateUserMaterial("/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "法人身份证正面.png",openid,"法人身份证正面") <= 0){
				mobileLoginService.intoUserMaterial("法人身份证正面","/api/"+realPath.substring(realPath.indexOf("userfiles"),realPath.length()) + "法人身份证正面.png",openid);
			}else{
				mobileLoginService.delUserMaterial(openid);
			}
		} catch (Exception e) {
			log.error(openid+"上传法人身份证正面失败！reson:"+e.getMessage());
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * 更新历史材料库群众信息
	 *
	 * @author
	 * @version 2020-04-24
	 */
	@Async()
	public void updateUserMaterialHistory(String loginId){
		dockingOtherService.updateUserMaterialHistory(loginId);
	}


	/**
	 * 添加排队办理信息
	 *
	 * @author azj
	 * @version 2020-04-24
	 */
	public void taskUserValue() {
		List<String> userIdQZK = mobileOtherService.findUserIdQZK();
		for(String str : userIdQZK){
			mobileOtherService.updateOtherUserValue(mobileQueueService.findUserByQueueId(str));
		}
	}
}
